function u=pdecode_pscd(y)
% PCparams structure is implicit parameter
%
% y    : Received bits in an AWGN (of noise variance N0/2, available via "PCparams")
% u    : Decoded message bits
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%



global PCparams;
N=PCparams.N;
n = PCparams.n;
PI=PCparams.PI;

% Initializing the likelihoods (i.e. the right end of the butterfly str)
PCparams.LLR = 0; %PCparams.BITS=-1;
initialLRs = -(4*sqrt(PCparams.Ec)/PCparams.N0) * y; 
PCparams.LLR(N:2*N-1) = initialLRs;
%PCparams.LLRpscd(:,n+1) = initialLRs;
% Explanation:
% ------------
%   y(i) = x(i) + n; x \in {+sqrt(Ec),-sqrt(Ec)}; n ~ Gaussian(0,N0/2)
%   LLR(i) = Pr{y(i) | x(i) = -sqrt(Ec)} / Pr{y(i) | x(i) = +sqrt(Ec)}
%   Pr(y|x) = (1/sqrt(2*pi* (N0/2))) * exp( (y-x)^2 / (2*(N0/2)) )
finalLRs = zeros(N,2);
d_hat = zeros(N,1);
for j=1:N

        %i = bitreversed(j-1,PCparams.n) +1 ; % "+1" is for base-1 indexing
        i= nextindex(PI,j-1)+1;
        updateLLR_pscd(i-1,0);
        finalLRs(i,1) = PCparams.LLR(1); %DEBUG
        finalLRs(i,2) = j;
    if PCparams.FZlookup(i) == -1
        %if PCparams.LLRpscd(i,1) > 0
        if PCparams.LLR(1) > 0
            d_hat(i) = 0;
        else
            d_hat(i) = 1;
        end
    else
        d_hat(i) = PCparams.FZlookup(i);
    end
    updateBITS_pscd(i-1,0);
end
%disp(PCparams.LLRpscd(:,1));
%disp(finalLRs);
u = d_hat ( PCparams.FZlookup == -1);
end